package org.minos.edit.view.components
{
	import org.aswing.AsWingConstants;
	import org.aswing.BorderLayout;
	import org.aswing.ext.Form;
	import org.aswing.ext.FormRow;
	import org.aswing.ext.GridList;
	import org.aswing.FlowLayout;
	import org.aswing.geom.IntDimension;
	import org.aswing.JButton;
	import org.aswing.JFrame;
	import org.aswing.JList;
	import org.aswing.JPanel;
	import org.aswing.JScrollPane;
	import org.aswing.JTable;
	import org.aswing.SoftBoxLayout;
	import org.aswing.table.PropertyTableModel;
	import org.aswing.table.sorter.TableSorter;
	import org.aswing.VectorListModel;
	import org.minos.edit.model.vo.AssetsVO;
	
	/**
	 * ...
	 * @author Minos
	 */
	public class SelectAssetsPane extends JPanel
	{
		private var scrollPane : JScrollPane;
		private var okButton : JButton;
		private var cancelButton : JButton;
		private var form : Form;
		private var pane : JPanel;
		private var list : JList;
		private var taskList : VectorListModel;
		private var tableModel : PropertyTableModel;
		private var sorter : TableSorter;
		private var table : JTable;
		protected var _items : Array;
		protected var _typeFilter : Array;
		private var _titles : Array;
		private var _binds : Array;
		
		public function SelectAssetsPane( items : Array , filter : Array , titles : Array = null , binds : Array = null )
		{
			super();
			_items = items?items:[];
			_typeFilter = filter?filter:[];
			_titles = titles;
			_binds = binds;
			init();
		}
		
		private function init() : void
		{
			setLayout( new BorderLayout() );
			//pane = new JPanel( new BorderLayout( 0 , 5 ) );
			//append( pane , BorderLayout.CENTER );
			table = new JTable();
			scrollPane = new JScrollPane( table );
			append( scrollPane , BorderLayout.CENTER );
			
			taskList = new VectorListModel();
			tableModel = new PropertyTableModel( taskList , _titles , _binds , [] );
			sorter = new TableSorter( tableModel );
			table.setModel( sorter );
			sorter.setTableHeader( table.getTableHeader() );
			
			tableModel.setColumnEditable( 1 , false );
			tableModel.setColumnEditable( 0 , false );
			
			for ( var i : int = 0 , len : int = items.length ; i < len ; i++ )
			{
				if ( items[ i ] && isResFile( items[ i ]["label"] ) )
					taskList.append( items[ i ] );
			}
			
			okButton = new JButton( "确定" );
			cancelButton = new JButton( "取消" );
			var buttonPane : JPanel = new JPanel( new FlowLayout( FlowLayout.CENTER , 16 , 5 ) );
			buttonPane.appendAll( okButton , cancelButton );
			
			append( buttonPane , BorderLayout.SOUTH );
		
		}
		
		protected function isResFile( v : String ) : Boolean
		{
			if ( !typeFilter )
				return false;
			for ( var i : uint = 0 ; i < typeFilter.length ; i++ )
			{
				if ( v.toLowerCase().indexOf( typeFilter[ i ] ) != -1 )
				{
					return true;
				}
			}
			return false;
		}
		
		public function getOkButton() : JButton
		{
			return okButton;
		}
		
		public function getCancelButton() : JButton
		{
			return cancelButton;
		}
		
		public function get items() : Array
		{
			return _items;
		}
		
		public function set items( value : Array ) : void
		{
			_items = value;
		}
		
		public function get typeFilter() : Array
		{
			return _typeFilter;
		}
		
		public function set typeFilter( value : Array ) : void
		{
			_typeFilter = value;
		}
		
		public function getSelectedVO() : AssetsVO
		{
			var row : int = table.getSelectedRow();
			var vo : AssetsVO = null;
			if ( row >= 0 )
			{
				vo = taskList.getElementAt( sorter.modelIndex( row ) );
			}
			return vo;
		}
		
		public function getSelectedVOs() : Array
		{
			var vos : Array = [];
			for ( var i : int = 0 , len : int = table.getSelectedRows().length ; i < len ; i++ )
			{
				vos.push( taskList.getElementAt( sorter.modelIndex( table.getSelectedRows()[ i ] ) ) );
			}
			return vos;
		}
	
	}

}